package com.leyou.user.web;

import com.leyou.auth.dto.UserDetail;


import com.leyou.auth.utils.UserContext;
import com.leyou.user.dto.UserDto;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/info")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 判断信息是否存在
     * @param data
     * @param type
     * @return
     */
    @GetMapping("/exists/{data}/{type}")
    public ResponseEntity<Boolean> exists(@PathVariable("data")String data,@PathVariable("type")Long type) {
        return ResponseEntity.ok(userService.exists(data,type));

    }

    /**
     * 根据用户名和密码查找用户
     * @param username
     * @param password
     * @return
     */
    @GetMapping
    public ResponseEntity<UserDto> queryUserByUsernameAndPassword(@RequestParam("username")String username, @RequestParam("password")String password) {

        return ResponseEntity.ok(userService.queryUserByUsernameAndPassword(username,password));
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @PostMapping("/code")
    public ResponseEntity<Void> sendCode(@RequestParam("phone")String phone) {

        userService.sendCode(phone);
        return ResponseEntity.ok().build();
    }
    /**
     * 注册
     */

    @PostMapping
    public ResponseEntity<Void> addUser(
            @RequestParam("username")String username,
            @RequestParam("password")String password,
            @RequestParam("phone")String phone,
            @RequestParam("code")String code
    ) {
        userService.addUser(username,password,phone,code);
        return ResponseEntity.ok().build();
    }


    @GetMapping("/me")
    public ResponseEntity<UserDetail> me() {
        UserDetail user = UserContext.getUser();
        System.out.println(user);
        return ResponseEntity.ok(UserContext.getUser());
    }

}
